import { Component, OnInit, Input } from '@angular/core';
import { NzImageService } from 'ng-zorro-antd/image';
import { NzMessageService } from 'ng-zorro-antd/message';
import { defalutImg } from 'src/app/comm';
import { MachineReportModalService } from './machine-report-modal.service';
import { purchaseChannelLists } from 'src/app/main/love-recyc-manage/machine-inspection-items';
import { machineResultArrs } from 'src/app/main/love-recyc-manage/love-recyc-manage.pipe';

@Component({
  selector: 'machine-report-modal',
  templateUrl: './machine-report-modal.component.html',
  styleUrls: ['./machine-report-modal.component.scss']
})
export class MachineReportModalComponent implements OnInit {
  // @Input() reportRequestData: any;

  fallback: string = defalutImg;
  isMask: boolean = true;
  isVisible: boolean = false;
  reportDetailDatas: any = {};

  recycleOrder: any = {}; // 用户评估
  phoneCheck: any = {}; // 前端验机报告
  checkNote: string = ''; // 验机师备注

  assessmentLists: any = []; // 评估list
  testImageLists: any = []; // 验机图片
  testTermLists: any = []; // 检测项

  // 购买渠道
  payChannelRadios: any = [...purchaseChannelLists];
  // 验机结果
  machineResultLists: any = [...machineResultArrs];

  constructor(
    private nzImageService: NzImageService,
    private message: NzMessageService,
    private modalServices: MachineReportModalService
  ) { }

  ngOnInit(): void {
  }

  /**
   * 查看验机报告
   * @param params { id: 订单ID、okey: 订单编号、phoneCheckId: 机器ID }
   */
  async loadData(params: any) {
    const res: any = await this.modalServices.getReportDeatilData(params);
    if (res.code == 0) {
      const { recycleOrder, phoneCheck, checkNote } = res.data || {};
      this.recycleOrder = recycleOrder || {};
      this.phoneCheck = phoneCheck || {};
      this.checkNote = checkNote || '-';

      this.assessmentLists = recycleOrder?.ueval ? JSON.parse(recycleOrder?.ueval) : [];
      this.testImageLists = phoneCheck?.img ? phoneCheck?.img.split(',') : [];
      this.testTermLists = phoneCheck?.checkContext ? JSON.parse(phoneCheck?.checkContext) : [];
    } else {
      this.recycleOrder = {};
      this.phoneCheck = {};
      this.checkNote = '-';
      this.assessmentLists = [];
      this.testImageLists = [];
      this.testTermLists = [];
      this.message.error(res.message);
    }
  }

  // 购买渠道filter
  payChannelFilter() {
    let tmpObj: any = this.payChannelRadios.find((item: any) => {
      return item.value == this.phoneCheck?.buyType;
    })
    return tmpObj?.label || '-';
  }

  // 验机结果filter
  resultFilter(type: any) {
    if (type) {
      let tmpObj: any = this.machineResultLists.find((item: any) => item.id == type);
      return tmpObj?.label || '';
    } else {
      return '';
    }
  }

  // 检测项图片预览
  previewImgs(imgList: Array<any>): void {
    let images: any = [];
    if (imgList.length) {
      imgList.forEach((img: any) => {
        images.push({
          src: img,
          alt: '检测图片'
        })
      })
    }
    this.nzImageService.preview(images, { nzZoom: 1.5, nzRotate: 0 });
  }
}
